// pages/edit-address/edit-address.js
const app = getApp();
import {
    areaList
} from '../../miniprogram_npm/miniprogram_npm/@vant/area-data/index';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: false, //地址弹框
        areaList, //area组件地址列表
        aid: null, //地址id
        username: null, //姓名
        tel: null, //电话,
        area: null, //地区
        province: null, //省份,
        city: null, //市,
        county: null, //区县,
        addressDetail: null, //详细地址,
        areaCode: null, //地区编号,
        postalCode: null, //邮政编码,
        isDefault: 0, //默认地址
        checked: false //开关
    },
    onLoad(options) {
        let item = JSON.parse(options.item)
        console.log(item);
        let checked = this.data.checked
        this.setData({
            aid: item.aid, //id
            username: item.name, //姓名
            tel: item.tel, //电话,
            area: item.province + '/' + item.city + '/' + item.county, //地区
            province: item.province, //省份,
            city: item.city, //市,
            county: item.county, //区县,
            addressDetail: item.addressDetail, //详细地址,
            areaCode: item.areaCode, //地区编号,
            postalCode: item.postalCode, //邮政编码,
            isDefault: item.isDefault, //默认地址
        })
    },
    onShow() {
        let checked = this.data.checked
        if (this.data.isDefault == 1) {
            checked = true
        } else {
            checked = false
        }
        this.setData({
            checked: checked
        })
    },
    // 导航栏事件
    // onClickLeft() {
    //     wx.showToast({
    //         title: '点击返回',
    //         icon: 'none'
    //     });
    // },

    // 开关事件
    onChange() {
        let checked = this.data.checked
        let isDefault = this.data.isDefault
        checked = !checked
        if (checked) {
            isDefault = 1
        } else {
            isDefault = 0
        }
        this.setData({
            checked: checked,
            isDefault: isDefault
        })
        console.log(this.data.checked);
        console.log(this.data.isDefault);
    },
    // 显示地址弹框
    showPopup() {
        this.setData({
            show: true
        });
    },
    // 隐藏地址弹框
    onClose() {
        this.setData({
            show: false
        });
    },
    // 地区确认
    confirm(e) {
        let arr = e.detail.values
        let area = ''
        for (let i = 0; i < arr.length; i++) {
            if (i == 2) {
                area += arr[i].name
            } else {
                area += arr[i].name + '/'
            }
        }
        this.setData({
            area: area,
            province: arr[0].name,
            city: arr[1].name,
            county: arr[2].name,
            areaCode: arr[2].code,
        })
    },
    // 保存地址
    save() {
        if (!this.data.username ||
            !this.data.tel ||
            !this.data.area ||
            !this.data.addressDetail ||
            !this.data.postalCode) {
            wx.showToast({
                title: '请全部输入',
                icon: 'error'
            })
        } else {
            wx.request({
                url: 'http://www.kangliuyong.com:10002/editAddress',
                method: "POST",
                header: {
                    "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
                },
                data: {
                    appkey: app.globalData.appkey,
                    tokenString: app.globalData.token,
                    aid: this.data.aid,
                    name: this.data.username,
                    tel: this.data.tel,
                    province: this.data.province,
                    city: this.data.city,
                    county: this.data.county,
                    addressDetail: this.data.addressDetail,
                    areaCode: this.data.areaCode,
                    postalCode: this.data.postalCode,
                    isDefault: this.data.isDefault
                },
                success: res => {
                    console.log(res);
                    if (res.data.msg == "编辑地址成功") {
                        wx.showToast({
                            title: '保存地址成功',
                        })
                        wx.navigateTo({
                            url: '/pages/address/address',
                        })
                    }
                }
            })
        }
    },
    del() {
        wx.request({
            url: 'http://www.kangliuyong.com:10002/deleteAddress',
            method: "POST",
            header: {
                "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
            },
            data: {
                appkey: app.globalData.appkey,
                tokenString: app.globalData.token,
                aid: this.data.aid,
            },
            success: res => {
                console.log(res);
                if (res.data.msg == "删除地址成功") {
                    wx.showToast({
                        title: '删除地址成功',
                    })
                    wx.navigateTo({
                        url: '/pages/address/address',
                    })
                }
            }
        })
    }
})